# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=flac
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.3
pkgrel=3
pkgdesc="Free Lossless Audio Codec (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://flac.sourceforge.io/"
license=('custom:Xiph' 'LGPL' 'GPL' 'FDL')
depends=("${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libssp"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs")
#optdepends=("${MINGW_PACKAGE_PREFIX}-xmms: for using the xmms plugin")
makedepends=("${MINGW_PACKAGE_PREFIX}-nasm" "${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
options=('strip' 'staticlibs')
source=("https://downloads.xiph.org/releases/flac/flac-${pkgver}.tar.xz"
        "Do-not-assume-that-sys-param.h-defines-MIN-and-MAX.patch")
sha256sums=('213e82bd716c9de6db2f98bcadbc4c24c7e2efe8c75939a1a84e28539c4e1748'
            'feab3c619d3739c620c658bd8ae44a2f39d1f677238ce49606e0175469c1f99f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/xiph/flac/commit/4a43f2bd4d7e12b5d994f48e18cf014008d204ce
  patch -Np1 -i "${srcdir}/Do-not-assume-that-sys-param.h-defines-MIN-and-MAX.patch"
  ./autogen.sh
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  LDFLAGS+=" -lssp"

  ../${_realname}-${pkgver}/configure \
    --enable-shared \
    --enable-static \
    --disable-xmms-plugin \
    --disable-rpath \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-pic
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
}
